package com.supereye.platform.web.org.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.supereye.platform.common.PageData;
import com.supereye.platform.util.JacksonUtil;
import com.supereye.platform.web.org.dao.OrgDao;
import com.supereye.platform.web.org.model.SysOrg;

@Service
public class OrgService {
	@Resource 
	private OrgDao orgDao;
	
	public String getOrgTree() throws Exception{
		SysOrg param = new SysOrg();
		param.setParentId(1);
		List<SysOrg> list = orgDao.getOrgList(param);
		
		if(list==null || list.size()<1){
			throw new Exception("数据未初始化组织机构");
		}
		
		List<Map<String,Object>> mine = new ArrayList<Map<String,Object>>();
		for(SysOrg org:list){
			Map<String,Object> result = new HashMap<String,Object>();
			
			result.put("name", org.getOrgShortName());
			result.put("open", "true");
			result.put("id", org.getId());
			result.put("icon", "icons/ico_company.png");
			result.put("isHidden", true);
			
			this.generTree(org, result);
			
			mine.add(result);
		}
		
		String jsonString = JacksonUtil.toJSon(mine);
		return jsonString;
	}
	
	public void generTree(SysOrg org,Map<String,Object> gmap) throws Exception{
		SysOrg param = new SysOrg();
		param.setParentId(org.getId());
		List<SysOrg> subs = orgDao.getOrgList(param);
		List<Map<String,Object>> subList = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		for(SysOrg o:subs){
			map = new HashMap<String,Object>();
			map.put("name", o.getOrgShortName());
			map.put("open", "true");
			map.put("id", o.getId());
			map.put("icon", o.getIcon());
			
			param = new SysOrg();
			param.setParentId(o.getId());
			this.generTree(o,map);
			
			subList.add(map);
		}
		
		gmap.put("children", subList);
	}

	public void addOrg(SysOrg org) throws Exception{
		orgDao.addOrg(org);
	}


	public String getOrgListWithParent(SysOrg org) throws Exception{
		List<SysOrg> list = orgDao.getOrgListWithParent(org);
		PageData<SysOrg> pageData = new PageData<SysOrg>();
		//pageData.setTotal(28);
		pageData.setRows(list);
		return JacksonUtil.toJSon(pageData);
	}

	public void deleteOrgs(String[] orgIds) throws Exception{
		for(String orgId:orgIds){
			SysOrg param = new SysOrg();
			param.setParentId(Integer.parseInt(orgId));
			List<SysOrg> subs = orgDao.getOrgListWithParent(param);
			if(subs!=null && subs.size()>0){
				throw new Exception("机构["+subs.get(0).getParentOrgName()+"]存在子节点，不允许删除");
			}
		}
		orgDao.deleteOrgs(orgIds);
	}

	public String getOrgTreeForCombo() throws Exception{
		SysOrg param = new SysOrg();
		param.setParentId(0);
		List<SysOrg> list = orgDao.getOrgList(param);
		Map<String,Object> result = new HashMap<String,Object>();
		if(list==null || list.size()<1){
			throw new Exception("数据未初始化组织机构");
		}
		SysOrg rootOrg = list.get(0);
		
		result.put("text", rootOrg.getOrgShortName());
		result.put("state", "open");
		result.put("id", rootOrg.getId());
		
		this.generTreeForCombo(rootOrg, result);
		
		String jsonString = JacksonUtil.toJSon(result);
		return jsonString;
	}
	
	
	public void generTreeForCombo(SysOrg org,Map<String,Object> gmap) throws Exception{
		SysOrg param = new SysOrg();
		param.setParentId(org.getId());
		List<SysOrg> subs = orgDao.getOrgList(param);
		List<Map<String,Object>> subList = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		for(SysOrg o:subs){
			map = new HashMap<String,Object>();
			map.put("id", o.getId());
			map.put("text", o.getOrgShortName());
			map.put("state", "true");
			
			param = new SysOrg();
			param.setParentId(o.getId());
			this.generTreeForCombo(o,map);
			
			subList.add(map);
		}
		
		gmap.put("children", subList);
	}

	public void updateOrg(SysOrg org) throws Exception {
		orgDao.updateOrg(org);
	}
	
}
